module.exports = (sequelize, DataTypes) => {
    const goods = sequelize.import('./goods.model')
    const orderModel = sequelize.define('order', {
      order_id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        defaultValue:0
      },
      order_status: {
        type: DataTypes.ENUM('未发货','已发货','已揽件', '运输中', '派件中', '已签收'),
        allowNull: false,
        defaultValue:'未发货'
      },
      reuser_name: {
        type: DataTypes.STRING,
        allowNull: false,
      },
      reuser_addr: {
        type: DataTypes.STRING,
        allowNull: false
      },
      reuser_tel:{
        type: DataTypes.STRING,
        allowNull: false
      },
      deuser_name: {
        type: DataTypes.STRING,
        allowNull: false
      },
      deuser_addr: {
        type: DataTypes.STRING,
        allowNull: false
      },
      deuser_tel:{
        type: DataTypes.STRING,
        allowNull: false
      },
      car_id: {
        type: DataTypes.STRING,
        allowNull: true,
        defaultValue: null
      },
      order_type: {
        type: DataTypes.STRING,
        allowNull: false
      },
      price: {
        type: DataTypes.FLOAT,
        allowNull: false,
        validate: {
          isFloat: true
        }
      },
      estimate: {
        type: DataTypes.STRING,
        allowNull: true,
      },
    },{
      createdAt:'order_create_time',
      updatedAt:'order_finish_time',
      tableName:'order_info'
    });
    orderModel.afterCreate(async(order)=>{
      await goods.update({
        order_id:order.order_id,
      },{
        where:{
          goods_id:0
        }
      })
    })
    orderModel.hasMany(goods, {
      foreignKey: 'order_id',
      sourceKey: 'order_id',
      onUpdate: 'CASCADE',
      onDelete: 'CASCADE'
    },
    goods.belongsTo(orderModel, {
      foreignKey: 'order_id',
      sourceKey: 'order_id',
      onUpdate: 'CASCADE',
      onDelete: 'CASCADE'
        })
        )
    return orderModel
  }
